package com.smbss.controller;

import com.smbss.entity.User;
import com.smbss.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * 用户
 */
@Controller
@RequestMapping(value = "user")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 跳转初始页
     * @return
     */
    @RequestMapping(value = "/" , method = RequestMethod.GET)
    public String index(HttpServletRequest request){
        List<User> users = this.userService.findByAll();
        request.setAttribute("users",users);
        return "user/user";
    }

    /**
     *  添加测试
     * @param request
     * @return
     */
    @RequestMapping(value = "/add" , method = RequestMethod.GET)
    public String add(HttpServletRequest request){
        User user = new User();
        String uuid =  UUID.randomUUID().toString();
        user.setName(String.format("我是测试数据：%s",uuid));
        user.setPassword(uuid);
        this.userService.insert(user);
        return index(request);
    }

    /**
     * 事物测试
     * @param request
     * @return
     */
    @RequestMapping(value = "/addByTr" , method = RequestMethod.GET)
    public String addByTr(HttpServletRequest request){
        User user = new User();
        String uuid =  UUID.randomUUID().toString();
        user.setName(String.format("我是测试数据：%s",uuid));
        user.setPassword(uuid);
        this.userService.insetByTransactional(user);
        return index(request);
    }

}
